﻿using System;
using System.Linq;
using FluentAssertions;
using Moq;
using NUnit.Framework;

namespace NextCommerce.Web.Templates.XmlRepository.Tests {

	public class XmlRepositoryTests {

		[TestFixture]
		public class GivenAnXmlRepository {

			private IConfiguration GetTestConfiguration() {
				var configurationMock = new Mock<IConfiguration>(MockBehavior.Strict);
				var testXmlFile = "testTemplates.xml";
				configurationMock.Setup(configuration => configuration.TemplatesXmlFile).Returns(testXmlFile);
				return configurationMock.Object;
			}

			[Test]
			public void WhenRequestingAValidTemplate_ThenTheTemplateShouldBeLoaded() {

				// Given
				var xmlRepository = new XmlRepository(GetTestConfiguration());

				// When
				var templateName = "test2";
				var template = xmlRepository.GetTemplate(templateName);

				// Then
				template.Name.Should().Be(templateName);
				template.Contents.Should().Be(@"<b>Test 2</b>");

			}

			[Test]
			// [Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedException(typeof (ArgumentException))]
			public void WhenGettingAnInvalidTemplate_ThenAnErrorShouldOccur() {

				// Given
				var xmlRepository = new XmlRepository(GetTestConfiguration());

				// When
				var templateName = "testError";
				Action act = (() => xmlRepository.GetTemplate(templateName));

				// Then
				act.ShouldThrow<ArgumentException>()
					// .WithMessage(@"The template 'testError' was not found. Paramenter name: name");
					.And.ParamName.Should().Be("name");
			}


			[Test]
			public void WhenGettingAllTemplates_ThenAllTheTemplatesShouldBeLoaded() {

				// Given
				var xmlRepository = new XmlRepository(GetTestConfiguration());
				const int templatesCount = 3;

				// When
				var templates = xmlRepository.GetAll();

				// Then
				Assert.AreEqual(templatesCount, templates.Count());

			}

		}

	}

}